<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //1.创建一个xhr对象
      const xhr = new XMLHttpRequest()

      //2.使用open方法
      xhr.open('POST', 'http://ajax-api.itheima.net/api/data')

      //3.设置请求头
      //   xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

      xhr.setRequestHeader('Content-Type', 'application/json')

      //4.send
      //   xhr.send('username=admin&password=123456')
      const obj = {
        username: 'admin',
        password: '123456',
      }

      xhr.send(JSON.stringify(obj))

      //5.监听状态
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          console.log(JSON.parse(xhr.responseText))
        }
      }
    </script>
  </body>
</html>
